package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.MemberDao;
import ysq.xyg.model.Member;
import ysq.xyg.service.MemberService;
import ysq.xyg.util.BaseUtil;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-04-19 14:11
 **/
@Service("memberService")
public class MemberServiceImpl extends BaseServiceImpl<Member> implements MemberService {
    @Resource
    private MemberDao memberDao;
    @Autowired
    public void setMemberDao(MemberDao memberDao) {
        this.baseDao = memberDao;
    }
    //分页查询
    @Override
    public PageInfo<Member> pageSelectEntityCount(Member member, int pageSize, int pageNum){
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy("num desc");
        if(StringUtil.isNotEmpty(member.getNickName())){
            member.setNickName(BaseUtil.encodeBase64String(member.getNickName()));
        }
        List<Member> list=memberDao.selectEntityCount(member);
        return new PageInfo<Member>(list);
    }
    //分页查询
    @Override
    public PageInfo<Member> pageSelectEntityXFCount(Member member, int pageSize, int pageNum){
        PageHelper.startPage(pageNum,pageSize);
        if(StringUtil.isNotEmpty(member.getNickName())){
            member.setNickName(BaseUtil.encodeBase64String(member.getNickName()));
        }
        PageHelper.orderBy("allMoney desc");
        List<Member> list=memberDao.selectEntityXFCount(member);
        return new PageInfo<Member>(list);
    }
    //分页查询
    @Override
    public PageInfo<Member> pageInfo(Member member, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        //通用Example查询
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(member.getPhone())){
            criteria.andLike("phone","%"+member.getPhone()+"%");
        }
        if (StringUtil.isNotEmpty(member.getMemberIds())){
            String memberIds = member.getMemberIds();
            String[] memberIdArr = memberIds.split(",");
            List<String> memberIdList = new ArrayList<String>();
            if (memberIdArr != null && memberIdArr.length > 0){
                for (String memberId : memberIdArr) {
                    memberIdList.add(memberId);
                }
            }
            criteria.andNotIn("id", memberIdList);
        }
        if(StringUtil.isNotEmpty(member.getName())){
            criteria.andLike("name","%"+member.getName()+"%");
        }
        if(StringUtil.isNotEmpty(member.getNickName())){
            criteria.andLike("nickName", "%"+BaseUtil.encodeBase64String(member.getNickName())+"%");
        }
        if(StringUtil.isNotEmpty(member.getId())){
            criteria.andLike("id","%"+member.getId()+"%");
        }
        if(StringUtil.isNotEmpty(member.getIntroductUserId())){
            criteria.andEqualTo("introductUserId",member.getIntroductUserId());
        }
        if(StringUtil.isNotEmpty(member.getOneUserId())){
            criteria.andEqualTo("oneUserId",member.getOneUserId());
        }
        if(StringUtil.isNotEmpty(member.getCardNo())){
            criteria.andLike("cardNo","%"+member.getCardNo()+"%");
        }
        if(StringUtil.isNotEmpty(member.getGrade())){
            criteria.andEqualTo("grade",member.getGrade());
        }
        List<Member> list = memberDao.selectByExample(example);
        return new PageInfo<Member>(list);
    }

    //条件查询
    @Override
    public List<Member> list(Member member) {

        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(member.getPhone())){
            criteria.andLike("phone","%"+member.getPhone()+"%");
        }
        if(StringUtil.isNotEmpty(member.getName())){
            criteria.andEqualTo("name",member.getName());
        }
        if(StringUtil.isNotEmpty(member.getNickName())){
            criteria.andLike("nickName", "%"+BaseUtil.encodeBase64String(member.getNickName())+"%");
        }
        List<Member> list=memberDao.selectByExample(example);
        return list;
    }

    @Override
    public Member selectEntityByOpenid(String openid) {
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("openid",openid);
        List<Member> list=memberDao.selectByExample(example);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }
    @Override
    public Member selectEntityByUnionId(String unionid) {
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("unionid",unionid);
        List<Member> list=memberDao.selectByExample(example);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }
    @Override
    public Member selectEntityByCardNo(String cardNo) {
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("cardNo",cardNo);
        List<Member> list=memberDao.selectByExample(example);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }
    @Override
    public Member selectEntityByPhone(String phone) {
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("phone",phone);
        List<Member> list=memberDao.selectByExample(example);
        if(list!=null && list.size()>0){
            return list.get(0);
        }
        return null;
    }
    @Override
    public List<Member> selectMoney(String introductUserId) {
        return memberDao.selectMoney(introductUserId);
    }

    @Override
    public List<Member> selectEntity(Member member) {
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(member.getPhone())){
            criteria.andEqualTo("phone", member.getPhone());
        }
        List<Member> list=memberDao.selectByExample(example);
        return list;
    }

    @Override
    public int selectMaxId() {
        return memberDao.selectMaxId();
    }

    @Override
    public PageInfo<Member> pageMyUserInfo(String memberId, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Member> list = memberDao.selectMyUserEntity(memberId);
        return new PageInfo<Member>(list);
    }

    @Override
    public int selectMyUserCount(String memberId) {
        return memberDao.selectMyUserCount(memberId);
    }

    @Override
    public int selectMyUserByCardNoCount(String cardNo) {
        return memberDao.selectMyUserByCardNoCount(cardNo);
    }

    @Override
    public PageInfo<Member> pageEntityInfo(Member member, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<Member> list = memberDao.selectEntity(member);
        return new PageInfo<Member>(list);
    }

    //查询数量
    @Override
    public int selectMemberCount(Member member){
        return memberDao.selectMemberCount(member);
    }

}
